package com.gitee.huanminabc.fastboot.web.context;

import com.gitee.huanminabc.fastboot.web.servlet.InterfaceMethodDefDesc;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/2 00:40
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@Data
@Slf4j
public class ApiThreadContext {
    private FullHttpRequest request;
    private ChannelHandlerContext ctx;
    private String reqContentType;//请求的contentType
    private String url;
    private InterfaceMethodDefDesc interfaceMethodDefDesc;


    private ApiThreadContext(FullHttpRequest request, ChannelHandlerContext ctx, String contentType, String url) {
        this.request = request;
        this.ctx = ctx;
        this.reqContentType = contentType;
        this.url = url;
    }

    //响应给前端下载的时候,如果传输的是byte[] 或者InputStream 那么可以给这个值,表示响应的文件名
    //默认的协议是application/octet-stream只要不是需要在线预览那么全部类型的文件下载都是支持的
    private String respDownFileName="file-not-name";
    private Map<String, Object> respHeader=new HashMap<>();
    //扩展
    private Map<String,Object> extend=new HashMap<>();

    //线程上下文
    public static final ThreadLocal<ApiThreadContext> apiThreadContext = new ThreadLocal<>();

    //设置当前线程上下文
    public static void build(FullHttpRequest request, ChannelHandlerContext ctx, String contentType, String url) {
        ApiThreadContext.apiThreadContext.set(new ApiThreadContext(request, ctx, contentType, url));
    }

    //获取当前线程上下文
    public static ApiThreadContext get() {
        return apiThreadContext.get();
    }

    //清除当前线程上下文
    public static void clear() {
        apiThreadContext.remove();
    }
}
